﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Tile;
using TS_XY_Train_Repaire.ToolSystem;

namespace Tool_System_Client
{
    public partial class OrderList_Form : DevExpress.XtraEditors.XtraForm
    {
        DataTable m_oDataTable = new DataTable();
        Dictionary<int, bool> m_dicToolSelect = new Dictionary<int, bool>();

        public OrderList_Form()
        {
            InitializeComponent();

            this.WindowState = FormWindowState.Maximized;
            tileView1.OptionsBehavior.AllowSmoothScrolling = true;
        }

        private void Tool_Apply_Form_Load(object sender, EventArgs e)
        {
            InitData();
            SetupView();
        }

        protected virtual void InitData()
        {
            try
            {
                int iUserCode = int.Parse(DataCache.GetInstance().OUserInfo.SUserCode);
                m_oDataTable = DBManager.GetInstance().GetOrderByUser(iUserCode);
                m_oDataTable.Columns.Add("f_statusname", typeof(string));

                foreach (DataRow dataRow in m_oDataTable.Rows)
                {
                    int iStatus = int.Parse(dataRow["f_status"].ToString());
                    dataRow["f_statusname"] = Utility.GetOrderStateString((OrderStatus)iStatus);
                }

                gridControl1.DataSource = m_oDataTable;
            }
            catch { }
        }

        void SetupView()
        {
            try
            {
                int Cap2ValAnchor = 0;
                int Item2ItemAnchor = 3;

                // Setup tiles options
                tileView1.BeginUpdate();
                tileView1.OptionsTiles.RowCount = 3;
                tileView1.OptionsTiles.Padding = new Padding(20);
                tileView1.OptionsTiles.ItemPadding = new Padding(10);
                tileView1.OptionsTiles.IndentBetweenItems = 20;
                tileView1.OptionsTiles.ItemSize = new Size(340, 190);
                tileView1.Appearance.ItemNormal.ForeColor = Color.White;
                tileView1.Appearance.ItemNormal.BorderColor = Color.Transparent;
                tileView1.ColumnSet.GroupColumn = tileView1.Columns["f_statusname"];
                //Setup tiles template
                TileViewItemElement leftPanel = new TileViewItemElement();
                //TileViewItemElement splitLine = new TileViewItemElement();
                TileViewItemElement idCaption1 = new TileViewItemElement();
                TileViewItemElement idValue = new TileViewItemElement();
                TileViewItemElement nameCaption = new TileViewItemElement();
                TileViewItemElement nameValue = new TileViewItemElement();
                TileViewItemElement dateCaption = new TileViewItemElement();
                TileViewItemElement dateValue = new TileViewItemElement();
                //TileViewItemElement toolbox = new TileViewItemElement();
                //TileViewItemElement image = new TileViewItemElement();
                tileView1.TileTemplate.Add(leftPanel);
                //tileView1.TileTemplate.Add(splitLine);
                tileView1.TileTemplate.Add(idCaption1);
                tileView1.TileTemplate.Add(idValue);
                tileView1.TileTemplate.Add(nameCaption);
                tileView1.TileTemplate.Add(nameValue);
                tileView1.TileTemplate.Add(dateCaption);
                tileView1.TileTemplate.Add(dateValue);
                //tileView1.TileTemplate.Add(toolbox);
                //tileView1.TileTemplate.Add(image);
                //
                leftPanel.StretchVertical = true;
                leftPanel.Width = 340;
                leftPanel.TextLocation = new Point(-10, 0);
                leftPanel.Appearance.Normal.BackColor = Color.FromArgb(58, 166, 101);
                //
                //splitLine.StretchVertical = true;
                //splitLine.Width = 3;
                //splitLine.TextAlignment = TileItemContentAlignment.Manual;
                //splitLine.TextLocation = new Point(180, 0);
                //splitLine.Appearance.Normal.BackColor = Color.White;
                //
                idCaption1.Text = "序号";
                idCaption1.TextAlignment = TileItemContentAlignment.TopLeft;
                idCaption1.Appearance.Normal.FontSizeDelta = -1;
                //
                idValue.Column = tileView1.Columns["f_id"];
                idValue.AnchorElement = idCaption1;
                idValue.AnchorIndent = Cap2ValAnchor;
                idValue.MaxWidth = 100;
                idValue.Appearance.Normal.FontStyleDelta = FontStyle.Bold;
                idValue.Appearance.Normal.Font = new Font("Segoe UI Semilight", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                //
                nameCaption.Text = "领用人";
                nameCaption.AnchorElement = idValue;
                nameCaption.AnchorIndent = Item2ItemAnchor;
                nameCaption.Appearance.Normal.FontSizeDelta = -1;
                //
                nameValue.Column = tileView1.Columns["f_proposname"];
                nameValue.AnchorElement = nameCaption;
                nameValue.AnchorIndent = Cap2ValAnchor;
                nameValue.Appearance.Normal.FontStyleDelta = FontStyle.Bold;
                nameValue.Appearance.Normal.Font = new Font("Segoe UI Semilight", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                //
                dateCaption.Text = "领用时间";
                dateCaption.AnchorElement = nameValue;
                dateCaption.AnchorIndent = Item2ItemAnchor;
                dateCaption.Appearance.Normal.FontSizeDelta = -1;
                //
                dateValue.Column = tileView1.Columns["f_propostime"];
                dateValue.AnchorElement = dateCaption;
                dateValue.AnchorIndent = Cap2ValAnchor;
                dateValue.Appearance.Normal.FontStyleDelta = FontStyle.Bold;
                dateValue.Appearance.Normal.Font = new Font("Segoe UI Semilight", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                //
                //toolbox.Column = tileView1.Columns["f_toolboxname"];
                //toolbox.TextAlignment = TileItemContentAlignment.BottomLeft;
                //toolbox.Appearance.Normal.Font = new Font("Segoe UI Semilight", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                ////
                //image.Column = tileView1.Columns["Image"];
                //image.ImageSize = new Size(110, 110);
                //image.ImageAlignment = TileItemContentAlignment.MiddleRight;
                //image.ImageScaleMode = TileItemImageScaleMode.ZoomOutside;
                //image.ImageLocation = new Point(-10, 10);

            }
            finally
            {
                tileView1.EndUpdate();
            }
        }

        Color colorPanelApply = Color.FromArgb(158, 158, 158);
        Color colorPanelConfirm = Color.Orange;
        Color colorPanelReturn = Color.PowderBlue;
        Color colorPanelFinish = Color.DarkSeaGreen;
        Color colorCaption = Color.FromArgb(219, 219, 219);

        private void tileView1_ItemCustomize(object sender, TileViewItemCustomizeEventArgs e)
        {
            if (e.Item == null || e.Item.Elements.Count == 0)
                return;

            OrderStatus iStatus = (OrderStatus)tileView1.GetRowCellValue(e.RowHandle, tileView1.Columns["f_status"]);
            if (iStatus == OrderStatus.Apply)
                e.Item.Elements[0].Appearance.Normal.BackColor = colorPanelApply;
            else if (iStatus == OrderStatus.Confirm)
                e.Item.Elements[0].Appearance.Normal.BackColor = colorPanelApply;
            else if (iStatus == OrderStatus.Return)
                e.Item.Elements[0].Appearance.Normal.BackColor = colorPanelReturn;
            else if (iStatus == OrderStatus.Finish)
                e.Item.Elements[0].Appearance.Normal.BackColor = colorPanelFinish;
            else
                e.Item.Elements[0].Appearance.Normal.BackColor = colorPanelApply;

            e.Item.Elements[1].Appearance.Normal.ForeColor = colorCaption;
            e.Item.Elements[3].Appearance.Normal.ForeColor = colorCaption;
            e.Item.Elements[5].Appearance.Normal.ForeColor = colorCaption;
        }

        private void tileView1_ItemClick(object sender, TileViewItemClickEventArgs e)
        {
            //var sColumn = tileView1.Columns["f_id"];
            //if (sColumn == null)
            //    return;

            DataRow dataRow = (DataRow)tileView1.GetDataRow(e.Item.RowHandle);
            if (dataRow == null)
                return;

            OrderDetail_Form oForm = new OrderDetail_Form();
            oForm.m_oOrder.IID = (int)dataRow["f_id"];
            oForm.m_oOrder.IProposID = Utility.GetRowInt(dataRow, "f_proposid");
            oForm.m_oOrder.SProposName = Utility.GetRowString(dataRow, "f_proposname");
            oForm.m_oOrder.SProposTime = (DateTime)dataRow["f_propostime"];
            oForm.m_oOrder.ISendID = Utility.GetRowInt(dataRow, "f_sendid");
            oForm.m_oOrder.SSendName = Utility.GetRowString(dataRow, "f_sendname");
            oForm.m_oOrder.SSendTime = Utility.GetRowDatetime(dataRow, "f_sendtime");
            oForm.m_oOrder.IReciveID = Utility.GetRowInt(dataRow, "f_reciveid");
            oForm.m_oOrder.SReciveName = Utility.GetRowString(dataRow, "f_recivename");
            oForm.m_oOrder.SReciveTime = Utility.GetRowDatetime(dataRow, "f_recivetime");
            oForm.m_oOrder.IStatus = (OrderStatus)dataRow["f_status"];
            
            oForm.ShowDialog();
        }

        private void windowsUIButtonPanel1_ButtonClick(object sender, DevExpress.XtraBars.Docking2010.ButtonEventArgs e)
        {
            if (e.Button.Properties.Caption == "返回")
            {
                this.DialogResult = DialogResult.OK;
            }
            //else if (e.Button.Properties.Caption == "清空所选")
            //{
            //    InitData();
            //}
            //else if (e.Button.Properties.Caption == "确认提交")
            //{
            //    ApplyOrder();
            //}
        }
    }
}